<template>
      <el-container>
        <el-header>
          <div>
            <img src="../assets/logo.png" alt="后台管理系统"/>
            <span>后台管理系统</span>
          </div>
          <el-button type="info" @click="logout">退出系统</el-button>
        </el-header>
        <el-container>
          <el-aside :width="isCollapse ? '64px' : '180px'">
            <div class="toggleCollapse" v-text="isCollapse ? '>>>' : '<<<'" @click="toggleCollapse"></div>
            <el-menu background-color="#393D49" text-color="#fff" active-text-color="#1AA094" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
              <el-submenu v-for='item in list' :key='item.id' :index='item.id + ""'>
                <template slot="title">
                  <i :class="icons[item.id]"></i>
                  <span>{{item.authName}}</span>
                </template>
                <el-menu-item v-for='subItem in item.children' :index='"/" + subItem.path' :key='subItem.id' @click='saveActive("/" + subItem.path)'>
                  <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{subItem.authName}}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <el-main><router-view></router-view></el-main>
        </el-container>
      </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      isCollapse: true,
      list: [],
      icons: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  methods: {
    logout () {
      // 清除token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // 请求路径menus get方式
      let { data: res } = await this.$http.get('menus')
      // console.log(res)
      this.list = res.data
    },
    toggleCollapse () {
      this.isCollapse = !(this.isCollapse)
    },
    saveActive (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container{
  height: 100%;
}
.el-header{
  background-color: #393D49;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 5px solid #1AA094;
  .el-button{
    background-color: #393D49;
    border: 0;
  }
  div{
    height: 80%;
    display: flex;
    align-items: center;
    color: white;
    font-size: 24px;
    img{
      height: 100%;
      display: block;
      margin-left: 15px;
    }
    span{
      margin-left: 10px;
      display: block;
    }
  }
}
.el-aside{
  background-color: #393D49;
  .toggleCollapse{
    font-size: 14px;
    line-height: 20px;
    color: #909399;
    text-align: center;
  }
  .el-menu{
    border-right:none;
  }
  .iconfont{
    margin-right: 10px;
    font-size: 18px;
  }
}
.el-main{
  background-color: #F2F2F2;
}
</style>
